﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
using BasicForms;
using DevExpress.XtraCharts;
using GeneralLibs;

namespace BI
{
    public partial class UcCongNo : UserControl
    {
        private string _Title = "";
        public string Title
        {
            set
            {
                _Title = value;
                lblTitle.Text = value;
            }
            get
            {
                return _Title;
            }
        }

        public string TinhChatTaiKhoanMacDinh
        {
            set
            {
                try
                {
                    cmbTaiKhoan.EditValue = DanhMucCauHinhTaiKhoanMacDinh.Instance.GetTaiKhoanIDByTinhChat(value);
                }
                catch (Exception ex)
                {

                }
            }
        }

        public UcCongNo()
        {
            InitializeComponent();
        }

        private void UcCongNo_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            dtThoiDiem.DateTime = System.DateTime.Now;

            cmbTaiKhoan.DataSource = DanhMucTaiKhoan.Instance.DataView;
            cmbTaiKhoan.DisplayColumn = "SoHieu";
            cmbTaiKhoan.ValueColumn = "ID";
            cmbTaiKhoan.AddVisibleColumn("SoHieu", "Số hiệu", 100, 1);
            cmbTaiKhoan.AddVisibleColumn("Ten", "Tên", 300, 2);
            cmbTaiKhoan.EditValue = Guid.Empty;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            BasicForms.FrmProgressBarDev.StartShowProgress();
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = Config.TimeOut;

                DanhMucDoiTuongNghiepVu danhMucDoiTac = DanhMucDoiTuongNghiepVu.GetDanhMucDoiTac();
                int count = 0;

                ucOneSeriesChart1.Data.Clear();
                foreach (DoiTuongNghiepVu doiTuong in danhMucDoiTac)
                {
                    SoDuTheoDoiTuong soDu = SoDuTheoDoiTuong.CalculateFromDB(cmd, dtThoiDiem.DateTime, (Guid)cmbTaiKhoan.EditValue, doiTuong.ID);
                    if (soDu != null && Math.Abs(soDu.DuNo - soDu.DuCo) > 0)
                    {
                        DataRow row = ucOneSeriesChart1.Data.NewRow();
                        row["Series1Name"] = doiTuong.Ten;
                        row["Series1Value"] = (decimal)Math.Abs(soDu.DuNo - soDu.DuCo)/1000000;
                        ucOneSeriesChart1.Data.Rows.Add(row);
                    }
                    count++;
                    BasicForms.FrmProgressBarDev.Status = string.Format("Đã xử lý {0}/{1}", count, danhMucDoiTac.Count);
                }
                DataView dataView = ucOneSeriesChart1.Data.DefaultView;
                dataView.Sort = "Series1Value DESC";
                ucOneSeriesChart1.Data = dataView.ToTable();
                DataTable tableTemp = ucOneSeriesChart1.Data.Clone();

                const int MAX_ELEMENT_NUMBER = 20;
                int i = 0;
                for (; i < ucOneSeriesChart1.Data.Rows.Count && i <= MAX_ELEMENT_NUMBER; i++)
                {
                    DataRow row = tableTemp.NewRow();
                    tableTemp.Rows.Add(row);
                    row["Series1Name"] = ucOneSeriesChart1.Data.Rows[i]["Series1Name"];
                    row["Series1Value"] = ucOneSeriesChart1.Data.Rows[i]["Series1Value"];
                }
                if (i < ucOneSeriesChart1.Data.Rows.Count)
                {
                    DataRow row = tableTemp.NewRow();
                    tableTemp.Rows.Add(row);
                    row["Series1Name"] = "Đối tác khác";
                    row["Series1Value"] = (decimal)0;
                    for (; i < ucOneSeriesChart1.Data.Rows.Count; i++)
                    {
                        row["Series1Value"] = (decimal)row["Series1Value"] + (decimal)ucOneSeriesChart1.Data.Rows[i]["Series1Value"];
                    }
                }

                ucOneSeriesChart1.Data = tableTemp;

                ucOneSeriesChart1.GenChart("Công nợ", ViewType.Pie, "ĐV: triệu VNĐ");
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
            }

            BasicForms.FrmProgressBarDev.FinishShowProgress();
        }
    }
}
